package swc.smu.ui;

import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import swc.smu.ui.custom.SelectImageView;

public class AvatarActivity extends BaseActivity implements View.OnClickListener{
    Button cutButton;
    ImageView imageView;
    SelectImageView selectImageView;

    PointF startPoint = new PointF();
    PointF initPoint = new PointF();
    Matrix matrix = new Matrix();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_avatar);
        initView();
    }

    private void initView() {
        cutButton = (Button) findViewById(R.id.cut_button);
        cutButton.setOnClickListener(this);
        selectImageView = (SelectImageView) findViewById(R.id.select_dialog_listview);

        imageView = (ImageView) findViewById(R.id.avatar_image);
        imageView.setScaleType(ImageView.ScaleType.MATRIX); //必须设置该缩放方式 否则无法进行拖拽
        imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {

                    case MotionEvent.ACTION_DOWN:
                        startPoint.set(event.getX(), event.getY());
                        if(initPoint.x == 0 && initPoint.y == 0) {
                            initPoint.set(imageView.getX(), imageView.getY());
                        }
                        break;
                    case MotionEvent.ACTION_MOVE:
                        float x = event.getX() - startPoint.x;
                        float y = event.getY() - startPoint.y;
                        matrix.postTranslate(x, y);
                        startPoint.set(event.getX(), event.getY());

                        initPoint.set(initPoint.x + x, initPoint.y + y);
                        break;
                    case MotionEvent.ACTION_UP: //手指离开屏幕
                    case MotionEvent.ACTION_POINTER_UP: //多点触摸，有手指离开，但还有未离开的
                    case MotionEvent.ACTION_POINTER_DOWN: //多点触摸，多次触发触摸事件

                        break;

                }
                imageView.setImageMatrix(matrix);
                return true;
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cut_button:
                float x = imageView.getX();
                float y = imageView.getY();
                if(initPoint.x == 0 && initPoint.y == 0) {
                    initPoint.set(imageView.getX(), imageView.getY());
                }
                Rect imgRect = new Rect();
                imageView.getDrawingRect(imgRect);

                Rect rect = selectImageView.getCenterRect();
                int left = rect.left;
                int top = rect.top;
                int right = rect.right;
                int bottom = rect.bottom;

                Matrix m1 = imageView.getMatrix();
                Matrix m2 = imageView.getImageMatrix();

                Log.i("jss", "x==>" + x
                        + "--y==>" + y
                        + "--pointx==>" + initPoint.x
                        + "-->pointy==>" + initPoint.y
                        + "--> left==>"+left
                        + "--> right==>"+right
                        + "--> top==>"+top
                        + "--> bottom==>"+bottom
                        + "--> imgRect"+imgRect
                        + "--> m1" + m1
                        + "--> m2" + m2);

                break;
        }
    }
}
